<template>
  <div :class="['sidebar', { collapsed: isCollapse }]">
    <!-- Logo部分 -->
    <div class="logo">
      <el-icon v-if="isCollapse" size="28" class="logo-icon">
        <Monitor />
      </el-icon>
      <div v-else class="logo-content">
        <el-icon size="32" class="logo-icon-main">
          <Monitor />
        </el-icon>
        <span class="logo-text">AI知识问答</span>
        <span class="logo-subtitle">智能管理系统</span>
      </div>
    </div>

    <!-- 菜单 -->
    <el-menu
      :default-active="activeMenu"
      :collapse="isCollapse"
      :unique-opened="false"
      class="sidebar-menu"
      background-color="transparent"
      text-color="#e2e8f0"
      active-text-color="#ffffff"
      @select="handleMenuSelect"  
    >
      <!-- 固定菜单项 -->
      <el-menu-item index="/dashboard" class="menu-item">
        <el-icon class="menu-icon"><Monitor /></el-icon>
        <span>工作台</span>
        <div class="menu-highlight"></div>
      </el-menu-item>
      <el-menu-item index="/userList" class="menu-item">
        <el-icon class="menu-icon"><Document /></el-icon>
        <span>用户管理</span>
        <div class="menu-highlight"></div>
      </el-menu-item>
      <el-menu-item index="/documentList" class="menu-item">
        <el-icon class="menu-icon"><Document /></el-icon>
        <span>文档管理</span>
        <div class="menu-highlight"></div>
      </el-menu-item>
      <el-menu-item index="/knowledgeBase" class="menu-item">
        <el-icon class="menu-icon"><Grid /></el-icon>
        <span>知识库管理</span>
        <div class="menu-highlight"></div>
      </el-menu-item>
      <el-menu-item index="/sessionLog" class="menu-item">
        <el-icon class="menu-icon"><ChatDotRound /></el-icon>
        <span>对话记录</span>
        <div class="menu-highlight"></div>
      </el-menu-item>
      <el-menu-item index="/sessionStats">
        <el-icon class="menu-icon"><DataLine /></el-icon>
        <span>对话统计分析</span>
        <div class="menu-highlight"></div>
      </el-menu-item>
    </el-menu>

    <!-- 底部用户信息 -->
    <div class="sidebar-footer">
      <div class="user-info">
        <el-avatar size="small" :src="userAvatar" class="user-avatar">
          <el-icon><User /></el-icon>
        </el-avatar>
        <div v-if="!isCollapse" class="user-details">
          <span class="user-name">{{ userName }}</span>
          <span class="user-role">{{ roleDisplay }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { Monitor, Document, Grid, ChatDotRound, User, DataLine } from '@element-plus/icons-vue'
import { useAdminStore } from '@/stores/admin'

defineProps({
  isCollapse: { type: Boolean, default: false }
})

const route = useRoute()
const router = useRouter()
const adminStore = useAdminStore()

const activeMenu = computed(() => route.path)

const handleMenuSelect = (index) => {
  router.push(index)
}

// 当前登录者的信息
const userName = computed(() => adminStore.userName)
const userAvatar = computed(() => adminStore.userAvatar || '')
const roleDisplay = computed(() => {
  const roles = adminStore.userRoles || []
  if (!roles || roles.length === 0) return '未分配角色'
  const mapCn = {
    admin: '系统管理员',
    user: '普通用户',
    guest: '游客'
  }
  // 兼容英文代码与中文角色名
  const pretty = roles.map(r => mapCn[r] || r)
  return pretty.join('、')
})
</script>

<style scoped>
.sidebar {
  background: linear-gradient(180deg, #1a1a2e 0%, #16213e 100%);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  height: 100vh;
  flex-shrink: 0;
  width: 280px;
  box-shadow: 4px 0 20px rgba(0, 212, 255, 0.2);
  position: relative;
  z-index: 10;
  border-radius: 0 16px 16px 0;
  border-right: 1px solid rgba(0, 212, 255, 0.3);
  animation: neonGlow 5s ease-in-out infinite alternate;
  /* margin: 8px 0 8px 8px; */
}

.sidebar::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 2px;
  background: linear-gradient(90deg, transparent, #00d4ff, transparent);
  animation: neonSweep 8s ease-in-out infinite;
}

.sidebar.collapsed {
  width: 80px;
}

.logo {
  height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom: 1px solid rgba(0, 212, 255, 0.3);
  color: #ffffff;
  flex-shrink: 0;
  transition: all 0.3s ease;
  padding: 10px 20px;
  background: linear-gradient(135deg, rgba(0, 212, 255, 0.1) 0%, rgba(0, 153, 204, 0.1) 100%);
}

.logo-icon {
  color: #00d4ff;
  transition: all 0.3s ease;
  filter: drop-shadow(0 2px 4px rgba(0, 212, 255, 0.3));
}

.logo-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  transition: opacity 0.3s ease;
}

.logo-icon-main {
  color: #00d4ff;
  margin-bottom: 4px;
  filter: drop-shadow(0 2px 4px rgba(0, 212, 255, 0.3));
}

.logo-text {
  font-size: 26px;
  font-weight: 700;
  color: #ffffff;
  letter-spacing: 1px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.logo-subtitle {
  font-size: 15px;
  color: rgba(255, 255, 255, 0.8);
  font-weight: 400;
  letter-spacing: 0.5px;
}

.sidebar-menu {
  border-right: none;
  height: calc(100vh - 180px);
  overflow-y: auto;
  overflow-x: hidden;
  width: 100%;
  padding: 16px 12px;
}

.sidebar-menu:not(.el-menu--collapse) {
  width: 100%;
}

.sidebar-menu.el-menu--collapse {
  width: 100%;
}

/* 菜单项样式 */
:deep(.el-menu-item) {
  position: relative;
  height: 60px;
  line-height: 52px;
  margin: 8px 8px;
  border-radius: 12px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  font-size: 16px;
  font-weight: 500;
  background: rgba(255, 255, 255, 0.02);
  border: 1px solid transparent;
}

:deep(.el-menu-item:hover) {
  background: linear-gradient(135deg, rgba(59, 130, 246, 0.15) 0%, rgba(99, 102, 241, 0.15) 100%) !important;
  border-color: rgba(59, 130, 246, 0.3);
  transform: translateX(4px);
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.2);
}

:deep(.el-menu-item.is-active) {
  background: linear-gradient(135deg, rgba(59, 130, 246, 0.25) 0%, rgba(99, 102, 241, 0.25) 100%) !important;
  color: #ffffff !important;
  border-color: rgba(59, 130, 246, 0.5);
  box-shadow: 0 4px 16px rgba(59, 130, 246, 0.3);
}

.menu-icon {
  font-size: 24px;
  color: currentColor;
  margin-right: 16px;
  transition: all 0.3s ease;
  filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.2));
}

.menu-highlight {
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 4px;
  height: 0;
  background: linear-gradient(180deg, #3b82f6 0%, #6366f1 100%);
  border-radius: 0 4px 4px 0;
  transition: all 0.3s ease;
  box-shadow: 0 0 8px rgba(59, 130, 246, 0.5);
}

:deep(.el-menu-item.is-active) .menu-highlight {
  height: 70%;
}

/* 滚动条样式 */
.sidebar-menu::-webkit-scrollbar {
  width: 6px;
}

.sidebar-menu::-webkit-scrollbar-thumb {
  background: linear-gradient(180deg, rgba(59, 130, 246, 0.3) 0%, rgba(99, 102, 241, 0.3) 100%);
  border-radius: 3px;
  transition: all 0.3s ease;
}

.sidebar-menu::-webkit-scrollbar-thumb:hover {
  background: linear-gradient(180deg, rgba(59, 130, 246, 0.5) 0%, rgba(99, 102, 241, 0.5) 100%);
}

.sidebar-menu::-webkit-scrollbar-track {
  background-color: transparent;
}

/* 折叠状态下调整 */
.sidebar.collapsed :deep(.el-menu-item) {
  padding: 0 24px !important;
  justify-content: center;
  margin: 8px 8px;
}

.sidebar.collapsed .menu-icon {
  margin-right: 0;
}

.sidebar.collapsed :deep(.el-menu-item span) {
  display: none;
}

.sidebar.collapsed :deep(.el-menu-item .menu-highlight) {
  display: none;
}

/* 底部用户信息 */
.sidebar-footer {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 16px 20px;
  background: linear-gradient(135deg, rgba(30, 41, 59, 0.8) 0%, rgba(15, 23, 42, 0.8) 100%);
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
}

.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  transition: all 0.3s ease;
}

.user-info:hover {
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(255, 255, 255, 0.2);
  transform: translateY(-1px);
}

.user-avatar {
  border: 2px solid rgba(0, 212, 255, 0.3);
  transition: all 0.3s ease;
}

.user-info:hover .user-avatar {
  border-color: rgba(0, 212, 255, 0.5);
  transform: scale(1.05);
}

.user-details {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.user-name {
  font-size: 14px;
  font-weight: 600;
  color: #ffffff;
}

.user-role {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.8);
  font-weight: 400;
}

.sidebar.collapsed .sidebar-footer {
  padding: 16px 12px;
}

.sidebar.collapsed .user-details {
  display: none;
}

.sidebar.collapsed .user-info {
  justify-content: center;
  padding: 8px;
}
.sidebar.collapsed .logo-content {
  opacity: 0;
  position: absolute;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .sidebar {
    width: 240px;
  }
  
  .sidebar.collapsed {
    width: 64px;
  }
  
  .logo {
    height: 90px;
  }
  
  .logo-text {
    font-size: 20px;
  }
  
  .sidebar-menu {
    height: calc(100vh - 140px);
    padding: 12px 8px;
  }
  
  :deep(.el-menu-item) {
    height: 52px;
    line-height: 44px;
    font-size: 14px;
    margin: 6px 6px;
  }
  
  .menu-icon {
    font-size: 20px;
    margin-right: 12px;
  }
  .logo-content {
    gap: 6px;
  }
  .logo-subtitle {
    font-size: 13px;
  }
}

/* 霓虹灯动画关键帧 */
@keyframes neonGlow {
  0% {
    box-shadow: 4px 0 20px rgba(0, 212, 255, 0.2);
  }
  100% {
    box-shadow: 4px 0 20px rgba(0, 212, 255, 0.4), 0 0 30px rgba(0, 212, 255, 0.2);
  }
}

@keyframes neonSweep {
  0% {
    transform: translateX(-100%);
  }
  100% {
    transform: translateX(100%);
  }
}
</style>